﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Security.Cryptography;

namespace EncryptionTest2
{
    class Class1
    {
        public string EncryptString(string inputString, int dwKeySize, string xmlString)
        {
            RSACryptoServiceProvider rsaCryptoServiceProvider = new RSACryptoServiceProvider(dwKeySize);
            rsaCryptoServiceProvider.FromXmlString(xmlString);
            int keySize = dwKeySize / 8;
            byte[] bytes = Encoding.UTF32.GetBytes(inputString);
            int maxLength = keySize - 42;
            int dataLength = bytes.Length;
            int iterations = dataLength / maxLength;
            StringBuilder stringBuilder = new StringBuilder();
            for (int i = 0; i <= iterations; i++ )
            {
                byte[] tempBytes = new byte[(dataLength-maxLength*i > maxLength)?(maxLength):(dataLength-maxLength*i)];
                Buffer.BlockCopy(bytes, maxLength*i,tempBytes,0,tempBytes.Length);
                byte[] encryptedBytes = rsaCryptoServiceProvider.Encrypt(tempBytes,true);
                stringBuilder.Append(Convert.ToBase64String(encryptedBytes));
            }
            return stringBuilder.ToString();
        }

    }
}
